var vue = new Vue({
  el: '#talking',
  data: {
    multipleSelection: [],
    title: '',
    days: '',
    record: '',
    talkingData: [],
    meetingData: [],
    totalSize: 0,
    pageIndex: 1,
    pageSize: 10,
    totalSize1: 0,
    pageIndex1: 1,
    pageSize1: 10,
    pickerOptions1: {
      disabledDate(time) {
        return time.getTime() > Date.now()
      }
    },
    // 添加界面表单
    form: {
      id: '',
      date_meeting: '',
      content: ''
    },
    //表单验证规则
    rules: {
      date_meeting: [{
        required: true,
        message: '请选择周会议',
        trigger: 'change'
      }],
      content: [{
        required: true,
        message: '请输入会议主题',
        trigger: 'blur'
      }]
    },

    formLabelWidth: '120px',
    //对话框显示状态
    addVisible: false,
    inVisible: false

  },
  created: function() {
    this.getMeeting()
  },
  mounted() {
    this.initGrid()
    this.initGrid1()
  },

  methods: {
    // 添加记录
    add: function(id) {
      this.addVisible = true
    },

    // 发起
    startTalking: function(rid) {
      if (this.multipleSelection.length === 0) {
        this.$message({
          showClose: true,
          message: '请至少选择一条数据',
          type: 'warning'
        })
        return false
      }
      var ids = this.multipleSelection.map(item => item.id).join()
      if (confirm('确定发起吗')) {
        var that = this
        $.post('discussSD/startDiscussSD', {
          ids: ids
        }, function(result) {
          if (result.data) {
            that.$message({
              message: '发起成功',
              type: 'success'
            })
            that.initGrid()
          } else {
            that.$message({
              message: '发起失败',
              type: 'warning'
            })
          }
        })
      }
    },
    // 删除
    deleteTalking: function() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          showClose: true,
          message: '请至少选择一条数据',
          type: 'warning'
        })
        return false
      }
      var ids = this.multipleSelection.map(item => item.id).join()
      if (confirm('确定删除吗')) {
        var that = this
        $.post('discussSD/removeForBatch', {
          ids: ids
        }, function(result) {
          if (result.data) {
            that.$message({
              message: '删除成功',
              type: 'success'
            })
            that.initGrid()
          } else {
            that.$message({
              message: '删除失败',
              type: 'warning'
            })
          }
        })
      }
    },
    // 获取周会议
    initGrid1: function() {
      var that = this
      $.post('weekMeeting/listByPageByDate', {
        index: that.pageIndex1,
        size: that.pageSize1
      }, function(result) {
        that.totalSize1 = result.data.total
        that.meetingData = result.data.data
      })
    },
    // 初始化表格
    initGrid: function() {
      var that = this
      $.post('discussSD/listByPageByDate', {
        index: that.pageIndex,
        size: that.pageSize
      }, function(result) {
        that.totalSize = result.data.total
        that.talkingData = result.data.data
      })
    },
    //翻页
    handleSizeChange(val) {
      this.pageSize = val
      this.initGrid()
    },
    //改变每页显示条数
    handleCurrentChange(val) {
      this.pageIndex = val
      this.initGrid()
    },
    //内部插入表格每页几条
    handleSizeChange1(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize1 = val
      this.initGrid1()
    },
    //内部插入表格翻页
    handleCurrentChange1(val) {
      console.log(`当前页: ${val}`)
      this.pageIndex1 = val
      this.initGrid1()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    //添加会议
    addTalking: function() {
      var that = this
      $.post('discussSD/save', {
        id_meeting: that.form.id,
        content: that.form.content
      }, function(result) {
        if (result.data) {
          that.$message({
            message: '增加成功',
            type: 'success'
          })
          that.addVisible = false
          this.$refs['form'].resetFields()
          that.initGrid()
        } else {
          that.$message({
            message: '增加失败',
            type: 'warning'
          })
        }
      })
    },
    //关闭dialog前执行的方法
    handleClose(done) {
      var that = this
      this.$confirm('确认关闭？')
        .then(_ => {
          if (that.addVisible) {
            that.addVisible = false
            this.$refs['form'].resetFields()
          }
        })
        .catch(_ => {
        })
    },
    handleClose1(done) {
      var that = this
      that.inVisible = false
    },

    // 注入记录
    init(row) {
      this.form.date_meeting = row.date_meeting.toString()
      this.form.id = row.id
      this.inVisible = false
    },
    //添加表单验证
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addTalking()

        } else {

          return false
        }
      })
    }

  }
})
